package com.example.botany_api.controller;

import com.alibaba.fastjson.JSON;
import com.example.botany_api.bean.Meaty;
import com.example.botany_api.bean.MeatyType;
import com.example.botany_api.service.MeatyService;
import com.example.botany_api.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 漫画忧作
 * @Date 2022/11/2 23:54
 * @Version 1.0
 */
@RestController
public class MeatyController {
    @Autowired
    private MeatyService meatyService;
    //    管理员 获得类别列表
    @RequestMapping("/admin/meatyTypeList")
    public String meatyTypeList(@RequestBody HashMap req) {
        String type_name = req.get("type_name").toString();
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", meatyService.getMeatyType(type_name, (current - 1) * size, size));
        res.put("total", meatyService.getMeatyTypeTotal(type_name));

        return JSON.toJSONString(res);
    }
    //    管理员 添加分类数据
    @RequestMapping("/admin/add_MeatyType")
    public String add_MeatyType(@RequestBody MeatyType meatyType) {
        int i = meatyService.add_MeatyType(meatyType);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 修改分类数据
    @RequestMapping("/admin/up_MeatyType")
    public String up_MeatyType(@RequestBody MeatyType meatyType) {
        System.out.println(meatyType);
        int i = meatyService.up_MeatyType(meatyType);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 删除数据
    @RequestMapping("/admin/dl_MeatyType")
    public String dl_MeatyType(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = meatyService.dl_MeatyType(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }




    @Autowired
    OssUtil ossUtil;

    //存储图片地址
    @RequestMapping("/admin/setMeatyImg")
    public String addImg(MultipartFile file){
        //调用上传阿里云oss
        String str=ossUtil.checkImage(file);
        System.out.println(str);
        return str;
    }
    //    管理员 获得多肉列表
    @RequestMapping("/admin/meatyList")
    public String meatyList(@RequestBody HashMap req) {
        String name = req.get("name").toString();
        int type_id;
        String summary = req.get("summary").toString();
        if(req.get("type_id").toString().equals("")){
            type_id=0;
        }else{
            type_id= Integer.parseInt(req.get("type_id").toString());
        }
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", meatyService.getMeaty(name,summary,type_id,(current - 1) * size, size));
        res.put("total", meatyService.getMeatyTotal(name,summary,type_id));

        return JSON.toJSONString(res);
    }
    //    管理员 添加 多肉数据
    @RequestMapping("/admin/add_Meaty")
    public String add_Meaty(@RequestBody Meaty meaty) {
        int i = meatyService.add_Meaty(meaty);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 修改 多肉数据
    @RequestMapping("/admin/up_Meaty")
    public String up_Meaty(@RequestBody Meaty meaty) {
        System.out.println(meaty);
        int i = meatyService.up_Meaty(meaty);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 多肉删除数据
    @RequestMapping("/admin/dl_Meaty")
    public String dl_Meaty(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = meatyService.dl_Meaty(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
    //    管理员 所有分类类别
    @RequestMapping("/admin/optionMeatyList")
    public String optionList() {
        Map res = new HashMap();
        res.put("dataList", meatyService.getOptionList());

        return JSON.toJSONString(res);
    }

    //    管理员 所有分类类别
    @RequestMapping("/detailsAddViews")
    public String detailsAddViews(@RequestBody HashMap req) {
        int type = Integer.parseInt(req.get("type").toString());
        int id = Integer.parseInt(req.get("id").toString());
        Map res = new HashMap();

        if(type==1){
            res.put("dataList", meatyService.meatyAndType(id));
            meatyService.meatyAddViews(id);
        }
         else if(type==2){
            res.put("dataList", meatyService.curingAndType(id));
            meatyService.curingAddViews(id);
        }
        else if(type==3){
            res.put("dataList", meatyService.informationAndType(id));
            meatyService.informationAddViews(id);
        }
        else if(type==4){
            res.put("dataList", meatyService.soilAndType(id));
            meatyService.soilAddViews(id);
        }



        return JSON.toJSONString(res);
    }
    //    添加多肉植物收藏
    @RequestMapping("/meatyAddSave")
    public String meatyAddSave(@RequestBody HashMap req) {
        int m_id = Integer.parseInt(req.get("m_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        int i=meatyService.meatyAddSave(m_id, u_id);
        if(i>0){
            return "ok";
        }

        return "err";
    }

    //    判断多肉植物有没有收藏
    @RequestMapping("/isMeatyAddSave")
    public String isMeatyAddSave(@RequestBody HashMap req) {
        int m_id = Integer.parseInt(req.get("m_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        Map res = new HashMap();
        int i = meatyService.isMeatyAddSave(m_id, u_id);
        res.put("isSave", i);
        return JSON.toJSONString(res);
    }
    //    取消多肉植物收藏
    @RequestMapping("/meatyCancelSave")
    public String meatyCancelSave(@RequestBody HashMap req) {
        int m_id = Integer.parseInt(req.get("m_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        int i = meatyService.meatyCancelSave(m_id, u_id);
        if(i>0){
            return "ok";
        }

        return "err";
    }
    //    获得植物图库收藏数据
    @RequestMapping("/meatyListSave")
    public String meatyListSave(@RequestBody HashMap req) {
        int u_id = Integer.parseInt(req.get("u_id").toString());

        Map res = new HashMap();
        res.put("dataList", meatyService.meatyListSave(u_id));

        return JSON.toJSONString(res);
    }
    //    获得植物图库收藏数据
    @RequestMapping("/admin/selectMapMeaty")
    public String selectMap(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        String address = req.get("address").toString();
        String lng = req.get("lng").toString();
        String lat = req.get("lat").toString();
        int i=meatyService.selectMap(id,address,lng,lat);
        if(i>0){
            return "ok";
        }

        return "err";
    }
    //    获得周边植物原产地
    @RequestMapping("/getLatLngNameIdMeaty")
    public String getLatLngNameIdMeaty(@RequestBody HashMap req) {
        String lng = req.get("lng").toString();
        String lat = req.get("lat").toString();
        Map res = new HashMap();
        res.put("dataList", meatyService.getLatLngNameId(lng,lat));
        return JSON.toJSONString(res);
    }
}
